﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataAccess;

namespace iEN.Schedule
{
    public partial class ScheduleConf : System.Web.UI.Page
    {
        string strAccount = "";
        string strIP = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            strAccount = Session["LoginAccount"].ToString();
            strIP = Session["LoginIP"].ToString();

            if (Session["SysAdmin"].ToString() != "2")
            {
                UpdatePanel4.Visible = false;
                TabContainer1.Visible = false;
                lblMsg.Visible = true;
            }
            if (!Page.IsPostBack)
            {
                
                initialScheduleDevice();
                
            }
        }

        protected void initialScheduleDevice()
        {
            //tab device
            Panel7.Visible = false;
            Panel8.Visible = false;
            //temp test
            Panel5.Visible = false;
            //end temp test
            ddlDevice.Items.Clear();
            ddlDevice.Items.Add("---");

            DataTable dt = new DBAccess_CHTD().GetPowerMeterSupportSchedule();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem li = new ListItem(dt.Rows[i]["DeviceName"].ToString().Trim(), dt.Rows[i]["DeviceID"].ToString());
                ddlDevice.Items.Add(li);
            }
            ddlDevice.SelectedIndex = 0;

            //tab group
            Panel6.Visible = false;
            Panel9.Visible = false;
            //temp test
            Panel3.Visible = false;
            //end temp test
            ddlGroup.Items.Clear();
            ddlGroup.Items.Add("---");

            dt = new DBAccess_CHTD().GetGroupInformation();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem li = new ListItem(dt.Rows[i]["GroupName"].ToString().Trim(), dt.Rows[i]["GroupID"].ToString());
                ddlGroup.Items.Add(li);
            }
            ddlGroup.SelectedIndex = 0;
        }
        
        protected void imgTip_Click(object sender, ImageClickEventArgs e)
        {
            this.Panel2.Visible = !this.Panel2.Visible;
            if (Panel2.Visible == true)
                imgTip.ImageUrl = "~/images/info_hide.gif";
            else
                imgTip.ImageUrl = "~/images/info_show.gif";
        }

        #region protected void btnScheduleAdd_Click(object sender, EventArgs e) //新設備排程
        protected void btnScheduleAdd_Click(object sender, EventArgs e)
        {

            if (ddlDevice.SelectedValue == "" || ddlDevice.SelectedValue == "---")
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇設備')", true);
                return;
            }

            gvScheduleList.SelectedIndex = -1;
            Panel7.Visible = true;
            Panel8.Visible = false;
            //temp test
            Panel5.Visible = true;
            //end temp test

            ddlStartHour.Items.Clear();
            int i = 0;
            for (i = 0; i < 24; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlStartHour.Items.Add(li);
            }
            ddlStartHour.SelectedIndex = 0;

            ddlStartMin.Items.Clear();
            for (i = 0; i < 60; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlStartMin.Items.Add(li);
            }
            ddlStartMin.SelectedIndex = 0;

            ddlEndHour.Items.Clear();
            for (i = 0; i < 24; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlEndHour.Items.Add(li);
            }
            ddlEndHour.SelectedIndex = 0;

            ddlEndMin.Items.Clear();
            for (i = 0; i < 60; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlEndMin.Items.Add(li);
            }
            ddlEndMin.SelectedIndex = 0;

  
            ddlWay.SelectedIndex = 0;
        }
        #endregion

        #region protected void btnGroupAdd_Click(object sender, EventArgs e) //新群組排時程
        protected void btnGroupAdd_Click(object sender, EventArgs e)
        {

            if (ddlGroup.SelectedValue == "" || ddlGroup.SelectedValue == "---")
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇群組')", true);
                return;
            }

            gvGroupList.SelectedIndex = -1;
            Panel6.Visible = true;
            Panel9.Visible = false;
            //temp test
            Panel3.Visible = true;
            //end temp test

            ddlGroupStartHour.Items.Clear();
            int i = 0;
            for (i = 0; i < 24; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlGroupStartHour.Items.Add(li);
            }
            ddlGroupStartHour.SelectedIndex = 0;

            ddlGroupStartMin.Items.Clear();
            for (i = 0; i < 60; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlGroupStartMin.Items.Add(li);
            }
            ddlGroupStartMin.SelectedIndex = 0;

            ddlGroupEndHour.Items.Clear();
            for (i = 0; i < 24; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlGroupEndHour.Items.Add(li);
            }
            ddlGroupEndHour.SelectedIndex = 0;

            ddlGroupEndMin.Items.Clear();
            for (i = 0; i < 60; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlGroupEndMin.Items.Add(li);
            }
            ddlGroupEndMin.SelectedIndex = 0;


            ddlGroupWay.SelectedIndex = 0;
        }
        #endregion

        #region protected void gvSchedule_RowCommand(object sender, GridViewCommandEventArgs e)
        protected void gvSchedule_RowCommand(object sender, GridViewCommandEventArgs e)
        {   
            //取得第幾列被選取 
            Boolean flag;

            if (e.CommandName == "Del")
            {
                int iIndex = Convert.ToInt32(e.CommandArgument);
                int scheduleGroupId = Convert.ToInt32(this.gvScheduleList.Rows[iIndex].Cells[1].Text);
                int scheduleId = Convert.ToInt32(this.gvScheduleList.Rows[iIndex].Cells[2].Text);
                string strMemo = "排程名稱：" + this.gvScheduleList.Rows[iIndex].Cells[0].Text + " 設備名稱: " + ddlDevice.SelectedItem.Text;

                flag = new DBAccess_CHTD().DeleteSchedule(scheduleGroupId, scheduleId);

                if (flag == true)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 98, strMemo + " 設備排程刪除成功" + " (schedule group id = " + scheduleGroupId.ToString() + " schedule id = "+ scheduleId.ToString()+")", "1", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('設備排程刪除成功')", true);
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 98, strMemo + " 設備排程刪除失敗" + " (schedule group id = " + scheduleGroupId.ToString() + " schedule id = " + scheduleId.ToString()+")", "0", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('設備排程刪除失敗')", true);
                }
                DataTable dt = new DBAccess_CHTD().GetScheduleInformation(DBAccess_CHTD.ScheduleType.Device, Convert.ToInt32(ddlDevice.SelectedValue));
                gvScheduleList.DataSource = dt;
                gvScheduleList.DataBind();
            }
        }
        #endregion

        #region protected void gvGroup_RowCommand(object sender, GridViewCommandEventArgs e)
        protected void gvGroup_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //取得第幾列被選取 
            Boolean flag;

            if (e.CommandName == "Del")
            {
                int iIndex = Convert.ToInt32(e.CommandArgument);
                int scheduleGroupId = Convert.ToInt32(this.gvGroupList.Rows[iIndex].Cells[1].Text);
                int scheduleId = Convert.ToInt32(this.gvGroupList.Rows[iIndex].Cells[2].Text);
                string strMemo = "排程名稱：" + this.gvGroupList.Rows[iIndex].Cells[0].Text + " 群組名稱: " + ddlGroup.SelectedItem.Text;

                flag = new DBAccess_CHTD().DeleteSchedule(scheduleGroupId, scheduleId);

                if (flag == true)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 95, strMemo + " 群組排時程刪除成功" + " (schedule group id = " + scheduleGroupId.ToString() + " schedule id = " + scheduleId.ToString()+")", "1", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('群組排時程刪除成功')", true);
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 95, strMemo + " 群組排時程刪除失敗" + " (schedule group id = " + scheduleGroupId.ToString() + " schedule id = " + scheduleId.ToString()+")", "0", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('群組排時程刪除失敗')", true);
                }
                DataTable dt = new DBAccess_CHTD().GetScheduleInformation(DBAccess_CHTD.ScheduleType.Group, Convert.ToInt32(ddlGroup.SelectedValue));
                gvGroupList.DataSource = dt;
                gvGroupList.DataBind();
            }
        }
        #endregion

        protected void ddlDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel7.Visible = false;
            Panel8.Visible = false;
            //temp test
            Panel5.Visible = false;
            //end temp test
        }

        protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel6.Visible = false;
            Panel9.Visible = false;
            //temp test
            Panel3.Visible = false;
            //end temp test
        }

        #region protected void btnScheduleQuery_Click(object sender, EventArgs e) //查詢設備排程
        protected void btnScheduleQuery_Click(object sender, EventArgs e)
        {
            Panel7.Visible = false;
            Panel8.Visible = false;
            //temp test
            Panel5.Visible = true;
            //end temp test    
            if (ddlDevice.SelectedValue == "" || ddlDevice.SelectedValue == "---")
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇設備')", true);
                return;
            }

            DataTable dt = new DBAccess_CHTD().GetScheduleInformation(DBAccess_CHTD.ScheduleType.Device, Convert.ToInt32(ddlDevice.SelectedValue));
            gvScheduleList.DataSource = dt;
            gvScheduleList.DataBind();
            Session["DeviceSchedule"] = dt;
        }
        #endregion

        #region protected void btnGroupQuery_Click(object sender, EventArgs e) //查詢群組排時程
        protected void btnGroupQuery_Click(object sender, EventArgs e)
        {
            Panel6.Visible = false;
            Panel9.Visible = false;
            //temp test
            Panel3.Visible = true;
            //end temp test
            if (ddlGroup.SelectedValue == "" || ddlGroup.SelectedValue == "---")
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇設備')", true);
                return;
            }

            DataTable dt = new DBAccess_CHTD().GetScheduleInformation(DBAccess_CHTD.ScheduleType.Group, Convert.ToInt32(ddlGroup.SelectedValue));
            gvGroupList.DataSource = dt;
            gvGroupList.DataBind();
            Session["GroupSchedule"] = dt;
        }
        #endregion

        #region protected void btnNewYes_Click(object sender, EventArgs e) //確定新增設備排程
        protected void btnNewYes_Click(object sender, EventArgs e)
        {
            int start = 0, end = 0;

            try
            {
                if (ddlDevice.SelectedValue == "" || ddlDevice.SelectedValue == "---")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇設備')", true);
                    return;
                }

                if (tbNameNew.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請輸入排程名稱')", true);
                    return;
                }
                if (tbStartTime.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇開始時間')", true);
                    return;
                }
                if (tbEndTime.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇結束時間')", true);
                    return;
                }

                
                if (DateTime.Parse(tbStartTime.Text) > DateTime.Parse(tbEndTime.Text))
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('結束日期不能小於開始日期')", true);
                    return;
                }

                if (DateTime.Parse(tbStartTime.Text) == DateTime.Parse(tbEndTime.Text))
                {
                    start = Convert.ToInt32(ddlStartHour.SelectedValue) * 60 + Convert.ToInt32(ddlStartMin.SelectedValue);
                    end = Convert.ToInt32(ddlEndHour.SelectedValue) * 60 + Convert.ToInt32(ddlEndMin.SelectedValue);
                    if (start >= end)
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('斷電時間不能小於等於供電時間')", true);
                        return;
                    }
                }
                

                string strAccount = Session["LoginAccount"].ToString();
                string strIP = Session["LoginIP"].ToString();
                string strMemo = "排程名稱：" + tbNameNew.Text+ " 設備名稱: "+ddlDevice.SelectedItem.Text;

                DBAccess_CHTD.OperatorType type = DBAccess_CHTD.OperatorType.EveryDay;
                switch (ddlWay.SelectedValue)
                {
                    case "0":
                        type = DBAccess_CHTD.OperatorType.EveryDay;
                        break;
                    case "1":
                        type = DBAccess_CHTD.OperatorType.MondayToFriday;
                        break;
                    case "2":
                        type = DBAccess_CHTD.OperatorType.SaturdayToSunday;
                        break;
                    case "3":
                        type = DBAccess_CHTD.OperatorType.Saturday;
                        break;
                    case "4":
                        type = DBAccess_CHTD.OperatorType.Sunday;
                        break;
                }

                bool blnRet = new DBAccess_CHTD().InsertSchedule(DBAccess_CHTD.ScheduleType.Device, Convert.ToInt32(ddlDevice.SelectedValue), tbNameNew.Text, DateTime.Parse(tbStartTime.Text), DateTime.Parse(tbEndTime.Text), Convert.ToInt32(ddlStartHour.SelectedValue), Convert.ToInt32(ddlStartMin.SelectedValue), Convert.ToInt32(ddlEndHour.SelectedValue), Convert.ToInt32(ddlEndMin.SelectedValue), type);

                if (blnRet == false)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 97, strMemo + " 設備排程新增失敗", "0", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增失敗')", true);
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 97, strMemo + " 設備排程新增成功", "1", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增成功')", true);
                }
                Panel7.Visible = false;
                DataTable dt = new DBAccess_CHTD().GetScheduleInformation(DBAccess_CHTD.ScheduleType.Device, Convert.ToInt32(ddlDevice.SelectedValue));
                gvScheduleList.DataSource = dt;
                gvScheduleList.DataBind();
            }
            catch 
            {
                Panel7.Visible = false;
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增失敗')", true);
            }
        }
        #endregion

        #region protected void btnGroupNewYes_Click(object sender, EventArgs e) //確定新增群組排時程
        protected void btnGroupNewYes_Click(object sender, EventArgs e)
        {
            int start = 0, end = 0;

            try
            {
                if (ddlGroup.SelectedValue == "" || ddlGroup.SelectedValue == "---")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇群組')", true);
                    return;
                }

                if (tbGroupName.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請輸入排程名稱')", true);
                    return;
                }
                if (tbGroupStartTime.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇開始時間')", true);
                    return;
                }
                if (tbGroupEndTime.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇結束時間')", true);
                    return;
                }

                if (DateTime.Parse(tbGroupStartTime.Text) > DateTime.Parse(tbGroupEndTime.Text))
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('結束日期不能小於開始日期')", true);
                    return;
                }

                if (DateTime.Parse(tbGroupStartTime.Text) == DateTime.Parse(tbGroupEndTime.Text))
                {
                    start = Convert.ToInt32(ddlGroupStartHour.SelectedValue) * 60 + Convert.ToInt32(ddlGroupStartMin.SelectedValue);
                    end = Convert.ToInt32(ddlGroupEndHour.SelectedValue) * 60 + Convert.ToInt32(ddlGroupEndMin.SelectedValue);
                    if (start >= end)
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('斷電時間不能小於等於供電時間')", true);
                        return;
                    }
                }


                string strAccount = Session["LoginAccount"].ToString();
                string strIP = Session["LoginIP"].ToString();
                string strMemo = "排程名稱：" + tbGroupName.Text + " 群組名稱: " + ddlGroup.SelectedItem.Text;

                DBAccess_CHTD.OperatorType type = DBAccess_CHTD.OperatorType.EveryDay;
                switch (ddlGroupWay.SelectedValue)
                {
                    case "0":
                        type = DBAccess_CHTD.OperatorType.EveryDay;
                        break;
                    case "1":
                        type = DBAccess_CHTD.OperatorType.MondayToFriday;
                        break;
                    case "2":
                        type = DBAccess_CHTD.OperatorType.SaturdayToSunday;
                        break;
                    case "3":
                        type = DBAccess_CHTD.OperatorType.Saturday;
                        break;
                    case "4":
                        type = DBAccess_CHTD.OperatorType.Sunday;
                        break;
                }

                bool blnRet = new DBAccess_CHTD().InsertSchedule(DBAccess_CHTD.ScheduleType.Group, Convert.ToInt32(ddlGroup.SelectedValue), tbGroupName.Text, DateTime.Parse(tbGroupStartTime.Text), DateTime.Parse(tbGroupEndTime.Text), Convert.ToInt32(ddlGroupStartHour.SelectedValue), Convert.ToInt32(ddlGroupStartMin.SelectedValue), Convert.ToInt32(ddlGroupEndHour.SelectedValue), Convert.ToInt32(ddlGroupEndMin.SelectedValue), type);

                if (blnRet == false)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 94, strMemo + " 群組排時程新增失敗", "0", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增失敗')", true);
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 94, strMemo + " 群組排時程新增成功", "1", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增成功')", true);
                }
                Panel6.Visible = false;
                DataTable dt = new DBAccess_CHTD().GetScheduleInformation(DBAccess_CHTD.ScheduleType.Group, Convert.ToInt32(ddlGroup.SelectedValue));
                gvGroupList.DataSource = dt;
                gvGroupList.DataBind();
            }
            catch
            {
                Panel6.Visible = false;
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('新增失敗')", true);
            }
        }
        #endregion

        #region protected void btnModifyYes_Click(object sender, EventArgs e) //確定修改設備排程
        protected void btnModifyYes_Click(object sender, EventArgs e)
        {
            int start = 0, end = 0;

            try
            {
                if (ddlDevice.SelectedValue == "" || ddlDevice.SelectedValue == "---")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇設備')", true);
                    return;
                }

                if (tbNameM.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請輸入排程名稱')", true);
                    return;
                }
                if (tbStartTimeM.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇開始時間')", true);
                    return;
                }
                if (tbEndTimeM.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇結束時間')", true);
                    return;
                }

                if (DateTime.Parse(tbStartTimeM.Text) > DateTime.Parse(tbEndTimeM.Text))
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('結束日期不能小於開始日期')", true);
                    return;
                }

                if (DateTime.Parse(tbStartTimeM.Text) == DateTime.Parse(tbEndTimeM.Text))
                {
                    start = Convert.ToInt32(ddlStartHourM.SelectedValue) * 60 + Convert.ToInt32(ddlStartMinM.SelectedValue);
                    end = Convert.ToInt32(ddlEndHourM.SelectedValue) * 60 + Convert.ToInt32(ddlEndMinM.SelectedValue);
                    if (start >= end)
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('斷電時間不能小於等於供電時間')", true);
                        return;
                    }
                }
               

                string strAccount = Session["LoginAccount"].ToString();
                string strIP = Session["LoginIP"].ToString();
                string strMemo = "排程名稱：" + tbNameM.Text + " 設備名稱: " + ddlDevice.SelectedItem.Text;

                DBAccess_CHTD.OperatorType type = DBAccess_CHTD.OperatorType.EveryDay;
                switch (ddlWayM.SelectedValue)
                {
                    case "0":
                        type = DBAccess_CHTD.OperatorType.EveryDay;
                        break;
                    case "1":
                        type = DBAccess_CHTD.OperatorType.MondayToFriday;
                        break;
                    case "2":
                        type = DBAccess_CHTD.OperatorType.SaturdayToSunday;
                        break;
                    case "3":
                        type = DBAccess_CHTD.OperatorType.Saturday;
                        break;
                    case "4":
                        type = DBAccess_CHTD.OperatorType.Sunday;
                        break;
                }

                int scheduleGroupId = Convert.ToInt32(Session["ScheduleGroupId"]);
                int scheduleId = Convert.ToInt32(Session["ScheduleId"]);

                bool blnRet = new DBAccess_CHTD().UpdateSchedule(scheduleGroupId, scheduleId, tbNameM.Text, DateTime.Parse(tbStartTimeM.Text), DateTime.Parse(tbEndTimeM.Text), Convert.ToInt32(ddlStartHourM.SelectedValue), Convert.ToInt32(ddlStartMinM.SelectedValue), Convert.ToInt32(ddlEndHourM.SelectedValue), Convert.ToInt32(ddlEndMinM.SelectedValue), type);

                if (blnRet == false)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 99, strMemo + " 設備排程修改失敗" + " (schedule group id = " + scheduleGroupId.ToString() + " schedule id = " + scheduleId.ToString()+")", "0", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('修改失敗')", true);
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 99, strMemo + " 設備排程修改成功" + " (schedule group id = " + scheduleGroupId.ToString() + " schedule id = " + scheduleId.ToString()+")", "1", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('修改成功')", true);
                }
                Panel8.Visible = false;
                DataTable dt = new DBAccess_CHTD().GetScheduleInformation(DBAccess_CHTD.ScheduleType.Device, Convert.ToInt32(ddlDevice.SelectedValue));
                gvScheduleList.DataSource = dt;
                gvScheduleList.DataBind();
            }
            catch (Exception ex)
            {
                Panel8.Visible = false;
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('修改失敗')", true);
            }
        }
        #endregion

        #region protected void btnGroupModifyYes_Click(object sender, EventArgs e) //確定修改群組排時程
        protected void btnGroupModifyYes_Click(object sender, EventArgs e)
        {
            int start = 0, end = 0;

            try
            {
                if (ddlGroup.SelectedValue == "" || ddlGroup.SelectedValue == "---")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇群組')", true);
                    return;
                }

                if (tbGroupNameM.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請輸入排程名稱')", true);
                    return;
                }
                if (tbGroupStartTimeM.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇開始時間')", true);
                    return;
                }
                if (tbGroupEndTimeM.Text == "")
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('請選擇結束時間')", true);
                    return;
                }

                if (DateTime.Parse(tbGroupStartTimeM.Text) > DateTime.Parse(tbGroupEndTimeM.Text))
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('結束日期不能小於開始日期')", true);
                    return;
                }

                if (DateTime.Parse(tbGroupStartTimeM.Text) == DateTime.Parse(tbGroupEndTimeM.Text))
                {
                    start = Convert.ToInt32(ddlGroupStartHourM.SelectedValue) * 60 + Convert.ToInt32(ddlGroupStartMinM.SelectedValue);
                    end = Convert.ToInt32(ddlGroupEndHourM.SelectedValue) * 60 + Convert.ToInt32(ddlGroupEndMinM.SelectedValue);
                    if (start >= end)
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('斷電時間不能小於等於供電時間')", true);
                        return;
                    }
                }


                string strAccount = Session["LoginAccount"].ToString();
                string strIP = Session["LoginIP"].ToString();
                string strMemo = "排程名稱：" + tbGroupNameM.Text + " 群組名稱: " + ddlGroup.SelectedItem.Text;

                DBAccess_CHTD.OperatorType type = DBAccess_CHTD.OperatorType.EveryDay;
                switch (ddlGroupWayM.SelectedValue)
                {
                    case "0":
                        type = DBAccess_CHTD.OperatorType.EveryDay;
                        break;
                    case "1":
                        type = DBAccess_CHTD.OperatorType.MondayToFriday;
                        break;
                    case "2":
                        type = DBAccess_CHTD.OperatorType.SaturdayToSunday;
                        break;
                    case "3":
                        type = DBAccess_CHTD.OperatorType.Saturday;
                        break;
                    case "4":
                        type = DBAccess_CHTD.OperatorType.Sunday;
                        break;
                }

                int scheduleGroupId = Convert.ToInt32(Session["GroupScheduleGroupId"]);
                int scheduleId = Convert.ToInt32(Session["GroupScheduleId"]);

                bool blnRet = new DBAccess_CHTD().UpdateSchedule(scheduleGroupId, scheduleId, tbGroupNameM.Text, DateTime.Parse(tbGroupStartTimeM.Text), DateTime.Parse(tbGroupEndTimeM.Text), Convert.ToInt32(ddlGroupStartHourM.SelectedValue), Convert.ToInt32(ddlGroupStartMinM.SelectedValue), Convert.ToInt32(ddlGroupEndHourM.SelectedValue), Convert.ToInt32(ddlGroupEndMinM.SelectedValue), type);

                if (blnRet == false)
                {
                    new DBAccess().addEventLog(strAccount, strIP, 96, strMemo + " 群組排程修改失敗" + " (schedule group id = " + scheduleGroupId.ToString() + " schedule id = " + scheduleId.ToString()+")", "0", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('修改失敗')", true);
                }
                else
                {
                    new DBAccess().addEventLog(strAccount, strIP, 96, strMemo + " 群組排時程修改成功" + " (schedule group id = " + scheduleGroupId.ToString() + " schedule id = " + scheduleId.ToString()+")", "1", 0);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('修改成功')", true);
                }
                Panel9.Visible = false;
                DataTable dt = new DBAccess_CHTD().GetScheduleInformation(DBAccess_CHTD.ScheduleType.Group, Convert.ToInt32(ddlGroup.SelectedValue));
                gvGroupList.DataSource = dt;
                gvGroupList.DataBind();
            }
            catch (Exception ex)
            {
                Panel9.Visible = false;
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "alert('修改失敗')", true);
            }
        }
        #endregion

        protected void gvSchedule_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Panel7.Visible = false;
            Panel8.Visible = true;
            //temp test
            Panel5.Visible = true;
            //end temp test
            
            //initial dropdownlist
            ddlStartHourM.Items.Clear();
            int i = 0;
            for (i = 0; i < 24; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlStartHourM.Items.Add(li);
            }
           
            ddlStartMinM.Items.Clear();
            for (i = 0; i < 60; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlStartMinM.Items.Add(li);
            }

            ddlEndHourM.Items.Clear();
            for (i = 0; i < 24; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlEndHourM.Items.Add(li);
            }

            ddlEndMinM.Items.Clear();
            for (i = 0; i < 60; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlEndMinM.Items.Add(li);
            }
            //end initial dropdownlist

            tbNameM.Text = this.gvScheduleList.Rows[e.NewSelectedIndex].Cells[0].Text;
            Session["ScheduleGroupId"] = Convert.ToInt32(this.gvScheduleList.Rows[e.NewSelectedIndex].Cells[1].Text);
            Session["ScheduleId"] = Convert.ToInt32(this.gvScheduleList.Rows[e.NewSelectedIndex].Cells[2].Text); ;
            tbStartTimeM.Text = this.gvScheduleList.Rows[e.NewSelectedIndex].Cells[3].Text;
            tbEndTimeM.Text = this.gvScheduleList.Rows[e.NewSelectedIndex].Cells[4].Text;

            string tmpstr = this.gvScheduleList.Rows[e.NewSelectedIndex].Cells[5].Text;
            if ((tmpstr.IndexOf("時") != -1) && (tmpstr.IndexOf("分") != -1))
            {
                tmpstr = tmpstr.Substring(0, tmpstr.IndexOf("時"));
                ddlStartHourM.SelectedIndex = Convert.ToInt32(tmpstr);

                tmpstr = this.gvScheduleList.Rows[e.NewSelectedIndex].Cells[5].Text;
                tmpstr = tmpstr.Substring(tmpstr.IndexOf("時") + 1, tmpstr.IndexOf("分") - tmpstr.IndexOf("時") - 1);
                ddlStartMinM.SelectedIndex = Convert.ToInt32(tmpstr);
            }

            tmpstr = this.gvScheduleList.Rows[e.NewSelectedIndex].Cells[6].Text;
            if ((tmpstr.IndexOf("時") != -1) && (tmpstr.IndexOf("分") != -1))
            {
                tmpstr = tmpstr.Substring(0, tmpstr.IndexOf("時"));
                ddlEndHourM.SelectedIndex = Convert.ToInt32(tmpstr);

                tmpstr = this.gvScheduleList.Rows[e.NewSelectedIndex].Cells[6].Text;
                tmpstr = tmpstr.Substring(tmpstr.IndexOf("時") + 1, tmpstr.IndexOf("分") - tmpstr.IndexOf("時") - 1);
                ddlEndMinM.SelectedIndex = Convert.ToInt32(tmpstr);
            }

            switch (this.gvScheduleList.Rows[e.NewSelectedIndex].Cells[7].Text)
            {
                case "每天":
                    ddlWayM.SelectedIndex = 0;
                    break;
                case "星期一至星期五":
                    ddlWayM.SelectedIndex = 1;
                    break;
                case "星期六至星期日":
                    ddlWayM.SelectedIndex = 2;
                    break;
                case "星期六":
                    ddlWayM.SelectedIndex = 3;
                    break;
                case "星期日":
                    ddlWayM.SelectedIndex = 4;
                    break;
            }
        }

        protected void gvGroup_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Panel6.Visible = false;
            Panel9.Visible = true;
            //temp test
            Panel3.Visible = true;
            //end temp test

            //initial dropdownlist
            ddlGroupStartHourM.Items.Clear();
            int i = 0;
            for (i = 0; i < 24; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlGroupStartHourM.Items.Add(li);
            }

            ddlGroupStartMinM.Items.Clear();
            for (i = 0; i < 60; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlGroupStartMinM.Items.Add(li);
            }

            ddlGroupEndHourM.Items.Clear();
            for (i = 0; i < 24; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlGroupEndHourM.Items.Add(li);
            }

            ddlGroupEndMinM.Items.Clear();
            for (i = 0; i < 60; i++)
            {
                ListItem li = new ListItem(i.ToString());
                ddlGroupEndMinM.Items.Add(li);
            }
            //end initial dropdownlist

            tbGroupNameM.Text = this.gvGroupList.Rows[e.NewSelectedIndex].Cells[0].Text;
            Session["GroupScheduleGroupId"] = Convert.ToInt32(this.gvGroupList.Rows[e.NewSelectedIndex].Cells[1].Text);
            Session["GroupScheduleId"] = Convert.ToInt32(this.gvGroupList.Rows[e.NewSelectedIndex].Cells[2].Text); ;
            tbGroupStartTimeM.Text = this.gvGroupList.Rows[e.NewSelectedIndex].Cells[3].Text;
            tbGroupEndTimeM.Text = this.gvGroupList.Rows[e.NewSelectedIndex].Cells[4].Text;

            string tmpstr = this.gvGroupList.Rows[e.NewSelectedIndex].Cells[5].Text;
            if ((tmpstr.IndexOf("時") != -1) && (tmpstr.IndexOf("分") != -1))
            {
                tmpstr = tmpstr.Substring(0, tmpstr.IndexOf("時"));
                ddlGroupStartHourM.SelectedIndex = Convert.ToInt32(tmpstr);

                tmpstr = this.gvGroupList.Rows[e.NewSelectedIndex].Cells[5].Text;
                tmpstr = tmpstr.Substring(tmpstr.IndexOf("時") + 1, tmpstr.IndexOf("分") - tmpstr.IndexOf("時") - 1);
                ddlGroupStartMinM.SelectedIndex = Convert.ToInt32(tmpstr);
            }

            tmpstr = this.gvGroupList.Rows[e.NewSelectedIndex].Cells[6].Text;
            if ((tmpstr.IndexOf("時") != -1) && (tmpstr.IndexOf("分") != -1))
            {
                tmpstr = tmpstr.Substring(0, tmpstr.IndexOf("時"));
                ddlGroupEndHourM.SelectedIndex = Convert.ToInt32(tmpstr);

                tmpstr = this.gvGroupList.Rows[e.NewSelectedIndex].Cells[6].Text;
                tmpstr = tmpstr.Substring(tmpstr.IndexOf("時") + 1, tmpstr.IndexOf("分") - tmpstr.IndexOf("時") - 1);
                ddlGroupEndMinM.SelectedIndex = Convert.ToInt32(tmpstr);
            }

            switch (this.gvGroupList.Rows[e.NewSelectedIndex].Cells[7].Text)
            {
                case "每天":
                    ddlGroupWayM.SelectedIndex = 0;
                    break;
                case "星期一至星期五":
                    ddlGroupWayM.SelectedIndex = 1;
                    break;
                case "星期六至星期日":
                    ddlGroupWayM.SelectedIndex = 2;
                    break;
                case "星期六":
                    ddlGroupWayM.SelectedIndex = 3;
                    break;
                case "星期日":
                    ddlGroupWayM.SelectedIndex = 4;
                    break;
            }
        }

        #region protected void NoEdit_Click(object sender, EventArgs e) //取消新增
        protected void NoEdit_Click(object sender, EventArgs e)
        {
            Panel7.Visible = false;
        }

        protected void NoEditDeviceM_Click(object sender, EventArgs e)
        {
            Panel8.Visible = false;
        }

        protected void NoEditGroup_Click(object sender, EventArgs e)
        {
            Panel6.Visible = false;
        }

        protected void NoEditGroupM_Click(object sender, EventArgs e)
        {
            Panel9.Visible = false;
        }
        #endregion

        protected void gvSchedule_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)  
            {  
                //要隱藏的欄位      
                e.Row.Cells[1].Visible = false;
                e.Row.Cells[2].Visible = false; 
            }   
        }

        protected void gvGroup_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
            {
                //要隱藏的欄位      
                e.Row.Cells[1].Visible = false;
                e.Row.Cells[2].Visible = false;
            }
        }
    }
}